import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Route } from '@angular/router';
import { CommonService } from 'src/app/service/common/common.service';
import { FeeEnquiryService } from 'src/app/service/fee-enquiry/fee-enquiry.service';
import { MessageService } from 'src/app/service/message.service';
import { downFile } from 'src/app/utils/utils';

@Component({
  selector: 'app-fee-bill',
  templateUrl: './fee-bill.component.html',
  styleUrls: ['./fee-bill.component.scss']
})
export class FeeBillComponent implements OnInit {

  usageCode = ''
  address = ''
  name = ''
  state: any = {
    un: { color: 'red', value: '未缴费'  },
    paying: { color: 'orange', value: '支付中' },
    done: { color: 'green', value: '已缴费' }
  }
  constructor(
    private feeEnquiry: FeeEnquiryService,
    private message: MessageService,
    private route: ActivatedRoute,
    private common: CommonService
  ) { }

  
  ngOnInit(): void {
    this.common.setRightInfoVisible(false)

    this.route.queryParams.subscribe((res: any) => {
      this.usageCode = res.usageCode
      this.address = res.address
      this.name = res.name
      this.getFeeBill()
    })
  }

  cols = {
    'month': '月份',
    'num': '水量(m³)',
    'amount': '水费(元)',
    'sewageAmount': '污水费(元)',
    'garbageFees': '垃圾费(元)',
    'total': '费用合计(元)',
    // 'state': '缴费状态',
    '操作': '操作',
  }
  tableData = []
  dataLoading = false
  total = 0
  pageNumber = 1
  pageSize = 10
  billId = ''
  getFeeBill(){
    this.dataLoading = true
    this.tableData = []
    // '00034987'
    this.feeEnquiry._getFeeBill(this.usageCode, this.pageNumber, this.pageSize).subscribe({
      next: res => {
        if(res && res.resultList){
          this.tableData = res.resultList.map((item: any) => {
            item.amount = parseFloat(item.amount)
            item.sewageAmount = parseFloat(item.sewageAmount)
            item.garbageFees = parseFloat(item.garbageFees)
            item.total = parseFloat(item.total)

            return item
          })
          this.total = res.totalNumber
          console.log(this.total)
        }
        this.dataLoading = false
      },
      error: error => {
        this.message.error('查询数据错误, 请稍后再试')
        this.dataLoading = false
      }
    })
  }

  billModal = false
  billUrl = ''
  billData: any = null
  eBill(data: any){
    console.log(data)
    this.billData = this._getBillData(data)
    this.dataLoading = true
    this.common._getPDF(this._getBillData(data)).subscribe((res) => {
      this.dataLoading = false
      // this.billUrl = res.fileId
      this.billUrl = URL.createObjectURL(res)
    }, error => {
      this.dataLoading = false
      this.message.createErrorMessage(error)
    })

  }

  btnLoading = false
  downBill(){
    this.btnLoading = true
    this.common._getPDF(this.billData).subscribe(res => {
      this.btnLoading = false
      downFile(res, '水费电子账单')
    }, error => {
      this.btnLoading = false
      this.message.createErrorMessage(error)
    })
  }

  _getBillData(data: any): FormData{
    const formData = new FormData()
    formData.append("modelName","bill")  //*** 必填，固定值bill
    Object.keys(data).forEach(key => {
      if(key.toLowerCase().indexOf('id') === -1){
        if(key === 'state'){
          // formData.append(key, this.state[data[key]].value)
        }else{
          formData.append(key, data[key] ? data[key] : '')
        }
      }
    })
    formData.append("name", this.name)
    // formData.append("address", this.address)
    return formData
  }

}

// formData.append("name","用户姓名")
// formData.append("address","用户地址")
// formData.append("code","用水编号")
// formData.append("month","202303")  //月份
// formData.append("lastMrDate","2023/02/01") //上次抄表时间
// formData.append("mrDate","2023/03/01")  //本次抄表时间
// formData.append("state","已缴费")  //需转换原参数值，un=未缴费，done=已缴费
// formData.append("num","23")  //本次抄表度数
// formData.append("preNum","12")  //上次抄表度数
// formData.append("quantity","13")  //水量
// formData.append("price","9.2")  //水价
// formData.append("amount","3453")  //水费金额
// formData.append("falsify","53")  //水费违约金
// formData.append("sewageQuantity","43")  //污水量
// formData.append("sewagePrice","3.5")  //污水价
// formData.append("sewageAmount","213")  //污水费金额
// formData.append("sewageFalsify","65.50")  //污水费违约金
// formData.append("total","3666")  //总金额
